// 导入dish表的model
const dishModel = require('../db/dish')
const xy = require('../utils/xy')
const writeErr = require('../utils/writeErr')

module.exports = {
    // 获取所有商品
    async getall(req, resp) {
        try{
            let docs = await dishModel.find()
            xy(resp, 200, '商品数据获取成功！', {
                dishes: docs
            })
        }catch(err) {
            writeErr(err)
            xy(resp, 505, '商品数据获取失败！')
        }
    },
    // 根据关键字获取对应的商品
    async search(req, resp) {
        // 获取传递过来的关键字
        let {keywords} = req.params
        // 根据关键字获取所有的商品
        try{
            let docs = await dishModel.find({name: new RegExp(keywords)})
            xy(resp, 200, '商品数据获取成功！', {
                dishes: docs
            })
        }catch(err) {
            writeErr(err)
            xy(resp, 505, '商品数据获取失败！')
        }
    },
    // 获取某个分类下的所有商品
    async catDish(req, resp) {
        let {catname} = req.params
        try{
            let docs = await dishModel.find({catname})
            xy(resp, 200, '商品数据获取成功！', {
                dishes: docs
            })
        }catch(err) {
            writeErr(err)
            xy(resp, 505, '商品数据获取失败！')
        }
    },

    // 后台
    async list(req, resp) {
        try{
            let dishlist = await dishModel.find()
            xy(resp, 200, '商品获取成功！', {
                dishlist: dishlist.reverse()
            })
        } catch(err) {
            writeErr(err)
            xy(resp, 535, '商品获取失败！')
        }
    },
    async del(req, resp) {
        let {id} = req.params
        try{
            await dishModel.deleteOne({_id: id})
            xy(resp, 200, '商品删除成功！')
        } catch(err) {
            writeErr(err)
            xy(resp, 534, '商品删除失败！')
        }
    },
    async add(req, resp) {
        let {name, price, imgpath, specifications, kind, enable, catname, detail} = req.body
        if(!name || !price || !imgpath || !specifications || !kind || enable === undefined || !catname || !detail) {
            xy(resp, 401, '缺少参数！')
            return
        }
        try{
            await dishModel.insertMany(req.body)
            xy(resp, 200, '商品新增成功！')
        } catch(err) {
            writeErr(err)
            xy(resp, 531, '商品新增失败！')
        }

    },
    async getOne(req, resp) {
        let {id} = req.params
        try{
            let dish = await dishModel.findOne({_id: id})
            xy(resp, 200, '获取商品成功！', {
                dish
            })
        }catch(err) {
            writeErr(err)
            resp(resp, 532, '获取商品失败！')
        }
    },
    async edit(req, resp) {
        let {id} = req.params
        let {name, price, imgpath, specifications, kind, enable, catname, detail} = req.body
        if(!name || !price || !imgpath || !specifications || !kind || enable === undefined || !catname || !detail) {
            xy(resp, 401, '缺少参数！')
            return
        }
        try{
            let dish = await dishModel.updateOne({_id: id}, req.body)
            xy(resp, 200, '修改商品成功！', {
                dish
            })
        }catch(err) {
            writeErr(err)
            xy(resp, 533, '修改商品失败！')
        }
    }
}